﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using UCL.Powersource.ViewModels;

namespace UCL.Powersource.Views
{
    /// <summary>
    /// Interaction logic for SettingsView.xaml
    /// </summary>
    public partial class SettingsView : UserControl
    {
        public SettingsViewModel VM { get { return (SettingsViewModel)(this.DataContext); } }

        public SettingsView()
        {
            InitializeComponent();
            this.Loaded += SettingsView_Loaded;
            this.Unloaded += SettingsView_Unloaded;
        }

        void SettingsView_Unloaded(object sender, RoutedEventArgs e)
        {
            VM.Deactivate();
        }

        void SettingsView_Loaded(object sender, RoutedEventArgs e)
        {
            if (VM != null)
            {
                VM.FireSaveDialog -= VM_FireSaveDialog;
                VM.FireSaveDialog += VM_FireSaveDialog;

                VM.FireDefaultDialog -= VM_FireDefaultDialog;
                VM.FireDefaultDialog += VM_FireDefaultDialog;

                VM.Activate();
            }
        }

        void VM_FireDefaultDialog(object sender, EventArgs e)
        {
            var button = MessageBoxButton.OKCancel;
            var icon = MessageBoxImage.Question;

            var result = System.Windows.MessageBox.Show(VM.DefaultDialogMsg, VM.DefaultDialogCaption, button, icon);

            switch (result)
            {
                case MessageBoxResult.OK:
                    VM.ResetToDefaults();
                    break;
                case MessageBoxResult.Cancel:
                    break;
            }
        }

        void VM_FireSaveDialog(object sender, EventArgs e)
        {
            var button = MessageBoxButton.YesNoCancel;
            var icon = MessageBoxImage.Warning;

            var result = System.Windows.MessageBox.Show(VM.SaveDialogMsg, VM.SaveDialogCaption, button, icon);

            switch (result)
            {
                case MessageBoxResult.Yes:
                    VM.SaveChanges();
                    break;
                case MessageBoxResult.No:
                    VM.Exit();
                    break;
                case MessageBoxResult.Cancel:
                    break;
            }
        }

        private void SelectIldasmFile(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog fileDialog = new Microsoft.Win32.OpenFileDialog();
            //fileDialog.DefaultExt = ".exe";
            //fileDialog.FileName = "ildasm.exe";
            fileDialog.Filter = "Disassemblers|ildasm.exe";
            fileDialog.InitialDirectory = IldasmPath.Text;

            bool? dialogResult = fileDialog.ShowDialog();
            
            if (dialogResult != null && (bool)dialogResult == true)
            {
                IldasmPath.Text = fileDialog.FileName;
            }

        }

        private void SelectDownloadPath(object sender, RoutedEventArgs e)
        {
            var dialog = FolderDialog(Environment.SpecialFolder.MyComputer, "Select the root folder for build downloads.");
            var result = dialog.ShowDialog();

            switch (result)
            { 
                case System.Windows.Forms.DialogResult.OK:
                    LocalDropBasePath.Text = dialog.SelectedPath;
                    break;
            }            
        }

        private void SelectExportPath(object sender, RoutedEventArgs e)
        {
            var dialog = FolderDialog(Environment.SpecialFolder.MyDocuments, "Select the folder for Excel exports.");
            var result = dialog.ShowDialog();

            switch (result)
            {
                case System.Windows.Forms.DialogResult.OK:
                    ExportPath.Text = dialog.SelectedPath;
                    break;
            }
        }

        private System.Windows.Forms.FolderBrowserDialog FolderDialog(Environment.SpecialFolder rootFolder, string description)
        {
            System.Windows.Forms.FolderBrowserDialog folderDialog = new System.Windows.Forms.FolderBrowserDialog();
            folderDialog.Description = description;
            folderDialog.ShowNewFolderButton = true;
            folderDialog.RootFolder = rootFolder;
            return folderDialog;

        }
    }
}
